package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    // 分页查询
    @Override
    public PageBean<SetmealVO> findByPage(SetmealPageDTO dto) {
        //1. 开启分页
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //2. 执行查询
        List<SetmealVO> list = setmealMapper.findList(dto);
        Page<SetmealVO> page = (Page<SetmealVO>) list;
        //3. 返回结果
        return new PageBean<>(page.getTotal(),page.getResult());
    }


    @Autowired
    private SetmealDishMapper setmealDishMapper;

    // 新增
    @Transactional
    @Override
    public void save(SetmealDTO dto) {
        //1. 先存储setmeal，主键返回
        Setmeal setmeal = BeanUtil.copyProperties(dto, Setmeal.class);
        setmealMapper.save(setmeal);
        //2. 再存储中间表
        //2-1 取出中间表集合
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        //2-2 遍历
        if (CollUtil.isNotEmpty(setmealDishes)) {
            setmealDishes.stream().forEach(setmealDish -> {
                //2-3 补齐套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //2-4 保存
                setmealDishMapper.save(setmealDish);
            });
        }
    }

    // 根据id查询套餐
    @Override
    public SetmealVO findById(Long id) {
        //1. 先查询套餐+分类名称
        SetmealVO setmealVO = setmealMapper.findById(id);
        //2. 再查询中间表菜品集合
        List<SetmealDish> setmealDishes = setmealDishMapper.findBySetmealId(id);
        //3. 组装并返回
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    // 修改套餐
    @Transactional
    @Override
    public void update(SetmealDTO dto) {
        //1. 先修改套餐
        Setmeal setmeal = BeanUtil.copyProperties(dto, Setmeal.class);
        setmealMapper.update(setmeal);
        //2. 先取出dto中菜品集合
        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        if (CollUtil.isNotEmpty(setmealDishes)) {
            //3. 先删除旧的菜品集合
            setmealDishMapper.deleteBySetmealId(setmeal.getId());
            //4. 重新添加新的菜品集合
            setmealDishes.stream().forEach(setmealDish -> {
                //4-1 补齐套餐id
                setmealDish.setSetmealId(setmeal.getId());
                //4-2 保存
                setmealDishMapper.save(setmealDish);
            });
        }

    }

    // 根据dto查询列表
    @Override
    public List<SetmealVO> findList(SetmealPageDTO dto) {
        return setmealMapper.findList(dto);
    }

    //根据套餐id查询菜品列表
    @Override
    public List<DishItemVO> findDishItemsBySetMealId(Long setmealId) {
        return setmealDishMapper.findDishItemsBySetmealId(setmealId);
    }
}
